using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Mvvm.Models;
using Mvvm.Services;
using Mvvm.ViewModels;

namespace Mvvm.ViewModels
{
    public class DepressionTestViewModel : PageViewModelBase, INotifyPropertyChanged
    {
        private readonly DepressionTestStorage _depressionTestStorage;
        private ObservableCollection<DepressionTestModel> _questions;
        private string _result;
        private int _score;
        private bool _canNavigateNext;
        private bool _canNavigatePrevious;

        public ObservableCollection<DepressionTestModel> Questions
        {
            get => _questions;
            set => SetProperty(ref _questions, value);
        }

        public string Result
        {
            get => _result;
            set => SetProperty(ref _result, value);
        }

        public int Score
        {
            get => _score;
            set => SetProperty(ref _score, value);
        }

        public override bool CanNavigateNext
        {
            get => _canNavigateNext;
            protected set => SetProperty(ref _canNavigateNext, value);
        }

        public override bool CanNavigatePrevious
        {
            get => _canNavigatePrevious;
            protected set => SetProperty(ref _canNavigatePrevious, value);
        }

        // 构造函数，注入 DepressionTestStorage
        public DepressionTestViewModel(DepressionTestStorage depressionTestStorage)
        {
            _depressionTestStorage = depressionTestStorage ?? throw new ArgumentNullException(nameof(depressionTestStorage));
            // 获取问题数据并初始化
            Questions = new ObservableCollection<DepressionTestModel>(_depressionTestStorage.GetQuestions());
        }

        // 提交测评
        public void SubmitCommand()
        {
            Score = CalculateScore();
            Result = GetAssessmentResult(Score);

            CanNavigateNext = true;
            CanNavigatePrevious = true;
        }

        // 计算得分
        private int CalculateScore()
        {
            // 每个问题根据用户选择的答案（SelectedAnswer）来计算得分
            int totalScore = 0;
            foreach (var question in Questions)
            {
                // 选项的索引值（0-3）代表得分
                totalScore += question.SelectedAnswer;
            }
            return totalScore;
        }

        // 获取评估结果
        private string GetAssessmentResult(int score)
        {
            // 根据得分区间返回评估结果和建议
            if (score <= 13)
            {
                return "轻度抑郁\n建议：\n" +
                       "1. 寻求支持：与朋友、家人或专业人士交流，获得情感支持和帮助。\n" +
                       "2. 保持活跃：定期进行身体锻炼，如散步或瑜伽，有助于提升心情。\n" +
                       "3. 养成健康习惯：保持规律作息，确保充足的睡眠。\n" +
                       "4. 冥想与放松：尝试冥想或深呼吸练习，减轻压力和焦虑。";
            }
            else if (score <= 19)
            {
                return "中度抑郁\n建议：\n" +
                       "1. 寻求专业帮助：考虑接受心理治疗或认知行为疗法（CBT）。\n" +
                       "2. 建立支持网络：与信任的人谈论你的感受，加入支持小组。\n" +
                       "3. 加强自我关怀：培养爱好，做冥想或冥想放松技巧，帮助减轻压力。\n" +
                       "4. 避免孤立：不要与社会隔离，主动参与社交活动。";
            }
            else if (score <= 28)
            {
                return "重度抑郁\n建议：\n" +
                       "1. 立刻寻求专业帮助：重度抑郁需要专业治疗，请尽早联系心理医生或精神科医生。\n" +
                       "2. 遵循治疗计划：严格按照医生的治疗建议，可能需要药物治疗和心理治疗。\n" +
                       "3. 注意安全：如有自杀念头或严重情绪困扰，立即寻求紧急帮助。\n" +
                       "4. 减少压力源：减少生活中的压力，适当调整工作和日常活动。";
            }
            else
            {
                return "极重度抑郁\n建议：\n" +
                       "1. 紧急医疗干预：极重度抑郁症常伴有自杀念头，请立即寻求紧急帮助。\n" +
                       "2. 住院治疗：在某些情况下，住院治疗可能是必要的，请尽快与医生联系。\n" +
                       "3. 建立支持系统：需要家人和朋友的持续支持，并在专业人员指导下提供帮助。\n" +
                       "4. 密切跟踪治疗进展：与治疗团队保持联系，定期评估治疗效果并进行调整。";
            }
        }


        // 实现 SetProperty 方法
        protected bool SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        // PropertyChanged 事件通知
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}















